package threading;

public class ThreadDemo3 {
    public static void main(String[] args) throws InterruptedException {
        Object object1 = new Object();
        Object object2 = new Object();
        Object object3 = new Object();
        Thread t1 = new Thread(()->{
                try {
                    for (int i = 0; i < 10; i++) {
                        synchronized (object1){
                            object1.wait();
                        }
                        System.out.print("A");
                        synchronized (object2) {
                            object2.notify();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        });

        Thread t2 = new Thread(()->{
                try {
                    for (int i = 0; i < 10; i++) {
                        synchronized (object2){
                            object2.wait();
                        }
                        System.out.print("B");
                        synchronized (object3) {
                            object3.notify();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        });

        Thread t3 = new Thread(()->{
                try {
                    for (int i = 0; i < 10; i++) {
                        synchronized (object3) {
                            object3.wait();

                        }
                        System.out.print("C");
                        System.out.println();
                        synchronized (object1) {
                            object1.notify();
                        }
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        });
        t1.start();
        t2.start();
        t3.start();
        Thread.sleep(10);
        synchronized (object1) {
            object1.notify();
        }
    }
}
